package com.pyb.common.response;

import com.pyb.common.upload.controller.model.GeneralJsonResult;

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * ResponseBodyWrapHandler
 *
 * @author chuchen.zhang
 * @date 17-2-26 上午1:52
 */
public class ResponseBodyWrapHandler implements HandlerMethodReturnValueHandler {

	private HandlerMethodReturnValueHandler processor;

	public ResponseBodyWrapHandler(HandlerMethodReturnValueHandler processor) {
		this.processor = processor;
	}

	public boolean supportsReturnType(MethodParameter returnType) {
		return processor.supportsReturnType(returnType);
	}

	public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
		if (!(returnValue instanceof GeneralJsonResult)) {
			GeneralJsonResult result = new GeneralJsonResult();
			result.setRet(true);
			result.setData(returnValue);
			returnValue = result;
		}
		processor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
	}
}
